#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/10/17 16:44
# @Author : 闫峰
# @File    : ceshi2.py
from datetime import datetime

import pymongo

from utils.mysqlUtils.mongodb_utils import MongoDBLink
import os

from common.setting import ensure_path_sep
from utils.logging_tool.log_control import LogHandler
from utils.mysqlUtils.db_pool import Database

current_path = os.path.abspath(__file__)
current_dir = os.path.dirname(current_path)
path_list = current_path.split("\\")
dis_path = "\\".join(path_list[-2:-1])
info_path = ensure_path_sep(f"\\logs\\info-{dis_path}.log")
error_path = ensure_path_sep(f"\\logs\\\error-{dis_path}.log")
warning_path = ensure_path_sep(f"\\logs\\warning-{dis_path}.log")
if os.path.exists(info_path):
    os.remove(info_path)
if os.path.exists(error_path):
    os.remove(error_path)
if os.path.exists(warning_path):
    os.remove(warning_path)
INFO = LogHandler(info_path, level='info')
ERROR = LogHandler(error_path, level='error')
WARNING = LogHandler(warning_path, level='warning')


class TestA:

    def test_(self, OrderSourceId):
        modb = MongoDBLink(
            uri='mongodb://rwuser:G1mob8U3VQ1obsFxHClJ!@124.71.9.215:8635,110.41.3.226:8635/?authSource=admin',
            dbname='irobotbox_marketpublish', coll_name='Ap_AutoPublish_Allegro_Listing')
        znkd_db = Database(ensure_path_sep("\\config\\znkd_mysql_config.yaml"), "znkd_database_prod")
        # 1 从mongodb筛选出要检查的数据
        start_date = datetime(2023, 10, 10, 0, 0, 0)
        filter = {
            "AddTime": {
                "$gte": start_date
            }
        }
        offset_start_id = 0
        while True:
            sql = """
            SELECT CustomerId,OrderSourceId,ProductId,CategoryId,id
            FROM m_allegro_listing
            WHERE OrderSourceId=%s
            AND OnlineStatus=1
            and id > %s
            ORDER BY id 
            LIMIT 10
            """

            res = znkd_db.fetch_all_tuple(sql, (OrderSourceId,offset_start_id))

            if len(res):
                offset_start_id = res[-1][4]
                for data in res:
                    is_true = self.get_code_is_required(data[3])
                    if is_true:
                        # 验证该listing的供应商编码是否修改  m_allegro_listing_update
                        pass
            else:
                break

    def get_code_is_required(self, category):
        znkd_db = Database(ensure_path_sep("\\config\\znkd_mysql_config.yaml"), "znkd_database_prod")
        get_sql = """
        SELECT id
        FROM m_allegro_attribute
        WHERE CustomerId=1  
        AND CategoryId=%s
        AND `Name` LIKE %s
        """
        a = "%Manufacturer's code%"
        res = znkd_db.fetch_all(get_sql, (category,a))
        if res:
            return True
        else:
            return False



if __name__ == '__main__':
    Test = TestA()
    Test.test_(9058)






if __name__ == '__main__':
    TestA().test_(11412)